import aiohttp
import asyncio
import re
import csv
import ssl


async def re_country(html):
    country = []
    a = re.compile(r'<span data-v-7f856186="">(.*?)</span>', re.MULTILINE | re.IGNORECASE)
    text = a.findall(html)
    for i in text:
        country_npc = i.replace('/', '')
        country.append(country_npc)
    return country


async def re_title(html):
    a = re.compile('<h2 data-v-7f856186="" class="m-b-sm">(.*?)</h2>', re.MULTILINE | re.IGNORECASE)
    title = a.findall(html)
    return title


async def http_requests(session, Url):
    async with session.get(Url, ssl=False) as response:
        html = await response.text()
        return await re_title(html), await re_country(html)


async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [http_requests(session, f"https://ssr1.scrape.center/page/{p}") for p in range(1, 10)]
        results = await asyncio.gather(*tasks)
        for result in results:
            titles, countries = result
            for i in zip(titles, countries):
                with open("man.csv", "a", encoding='utf-8') as f:
                    a = csv.writer(f)
                    a.writerow(i)
                    print(i)


if __name__ == "__main__":
    asyncio.run(main())
